import 'package:flutter/material.dart';
import 'package:route_manager/wx_navigator.dart';
import '../manager/timer_manager.dart';

// import '../../../global/global_import.dart';
// import '../../../global/application.dart';
// import '../../main/config/app_router.dart';
// import 'timer_manager.dart';
// import 'user_info_manager.dart';

// import '../../../business/position/provides/position_page_provider.dart';

class StateObserver extends WidgetsBindingObserver {
  StateObserver._();

  static StateObserver _observer = StateObserver._();

  factory StateObserver() => _observer;

  bool _hasRegister = false;

  void registerObserver() {
    // if(UserInfoManager().isLogin()) {
    if (!_hasRegister) {
      WidgetsBinding.instance.addObserver(this);
      _hasRegister = true;
    }

    // }
  }

  void removeObserver() {
    // if (_hasRegister) {
    _hasRegister = false;
    WidgetsBinding.instance.removeObserver(this);
    // }
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    // wxLog("state=====${state.toString()}");

    if (state == AppLifecycleState.paused) {
      TimerManager().lastTime = DateTime.now();
    } else if (state == AppLifecycleState.resumed) {
      if (TimerManager().isExpired()) {
        // UserInfoManager().resetUserInfo();

        // removeObserver();

        // clearCacheData();

        // Application.navigatorKey.currentState.pushNamedAndRemoveUntil(RoutePath.launchLogin, (route) => route == null);
        print('didChangeAppLifecycleState ------ ');
        WXNavigator.pushNamed(routeName: '/checkPINPage', arguments: {
          'isHiddenBack': false,
          'isExitAPP': true,
          'onCallBack': (String value) {
            WXNavigator.pop();
          }
        }
            // arguments: (value) {
            //   WXNavigator.pop();
            // }
            );
        // } else {

      }
    }

    super.didChangeAppLifecycleState(state);
  }

  /// token过期清除缓存
  clearCacheData() {
    // Provider.of<PositionProvider>(Application.navigatorKey.currentContext, listen: false).clear();
  }
}
